<template>
	<view class="my-total">
		<view class="one">
			<radio @click="changeAllState(!isChecked)" color="#c00000" :checked="isChecked"></radio>全选
		</view>
		<view class="two">合计:￥{{totalmoney}}</view>
		<view @click="pay" class="three">结算({{total}})</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-total",
		data() {
			return {
				// 定义一个定时器的倒计时
				second: 3,
				// 定义一个定时器
				timer: null
			};
		},
		computed: {
			...mapGetters('cart', ['total', 'isChecked', 'totalmoney']),
			...mapGetters('user', ['fullAddress']),
			...mapState('user', ['address', 'token']),
			...mapState('cart', ['cart'])
		},
		methods: {
			...mapMutations('cart', ['changeAllState', 'clearPayGoods']),
			// 倒计时提示的方法
			showTip(n) {
				uni.showToast({
					title: `您还未登录，${n}秒后跳转到登录页`,
					icon: 'none',
					duration: 2000
				})
			},
			delayNavigate() {
				// 调用时，将倒计时重置为 3
				this.second = 3
				this.showTip(this.second)
				// 设置一个定时器
				this.timer = setInterval(() => {
					// 过一秒，倒计时 -1
					this.second--
					if (this.second <= 0) {
						clearInterval(this.timer)
						return uni.switchTab({
							url: '../../pages/my/my'
						})
					}
					// 再次显示倒计时
					this.showTip(this.second)
				}, 1000)
			},
			async pay() {
				// 判断是否勾选了商品
				if (this.total <= 0) return uni.$showMsg('还未选择商品')
				// 判断是否选择了收货地址
				if (!this.address.userName) return uni.$showMsg('请选择收货地址')
				// 判断是否登录
				// 显示倒计时
				if (!this.token) return this.delayNavigate()
				// 商品结算
				// 生成订单所需要的商品数组
				const goods = []
				this.cart.forEach(item => {
					// 得到选中的商品
					if (item.goods_status) {
						goods.push({
							goods_id: item.goods_id,
							goods_number: item.goods_count,
							goods_price: item.goods_price
						})
					}
				})
				// --------------------------生成订单----------------------------
				const res = await uni.$http.post('/api/public/v1/my/orders/create', {
					order_price: this.totalmoney,
					consignee_addr: this.fullAddress,
					goods: goods
				})
				// console.log(res)
				if (res.data.meta.status !== 200) return uni.$showMsg()
				// 得到订单号
				const order_number = res.data.message.order_number
				// console.log(order_number)
				// --------------------------得到支付参数----------------------------
				const {
					data: payRes
				} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {
					order_number: order_number
				})
				// console.log(payRes)
				// 判断
				if (payRes.meta.status !== 200) return uni.$showMsg()
				// 得到支付参数
				const payData = payRes.message.pay
				// console.log(payData);
				// --------------------------开始支付----------------------------
				uni.requestPayment({
					...payData,
					success: (res) => {
						// 清除掉已支付的商品
						this.clearPayGoods()
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.my-total {
		display: flex;
		align-items: center;
		height: 120rpx;
		line-height: 120rpx;
		width: 100%;
		background-color: #fff;
		position: fixed;
		left: 0;
		bottom: 0;

		.one {
			padding: 0 10rpx;
		}

		.two {
			flex: 1;
			text-align: center;
		}

		.three {
			background-color: #c00002;
			width: 200rpx;
			text-align: center;
			color: #fff;
		}
	}
</style>
